//
//  KnowledgeViewController.m
//  HealthA段
//
//  Created by dllo on 15/6/3.
//  Copyright (c) 2015年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "KnowledgeViewController.h"
#import "KnowledgeClassCell.h"
#import "Knowledge.h"
#import "AFHTTPRequestOperationManager.h"
#import "KnowMessageViewController.h"
#import "Singleton.h"
#define HEIGHT self.view.frame.size.height
#define WIDTH self.view.frame.size.width
@interface KnowledgeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,retain)UICollectionView *knowledgeClassView;
@property(nonatomic,retain)NSMutableArray *knowArr;
@property(nonatomic,retain)NSArray *pivArr;
@end

@implementation KnowledgeViewController
- (void)dealloc
{
    [_knowArr release];
    [_knowledgeClassView release];
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.knowArr = [NSMutableArray array];
        self.pivArr = @[@"paobu.jpg",@"simishenghuo.jpg",@"nvxingbaoyang.jpg",@"nanxingjiankang",@"qinggan.jpg",@"yunyingshouce",@"yuerbaodian",@"jiankangyinshi",@"yaoxiang.jpg",@"ertong.jpg",@"laotou.jpg",@"taiyang.jpg",@"xinlijiankang.jpeg"];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"常识分类";
    [[Singleton shareSingleton] hiddenNO];
    [self creatKnowledgeClassView];
    [self PleaseData];
}
//创建知识分类视图
-(void)creatKnowledgeClassView
{
    UICollectionViewFlowLayout *folw = [[UICollectionViewFlowLayout alloc]init];
    folw.minimumInteritemSpacing = 10;
    folw.minimumLineSpacing = 10;
    folw.itemSize = CGSizeMake(0.28 *WIDTH, 0.4 *WIDTH);
    folw.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    folw.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.knowledgeClassView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 49) collectionViewLayout:folw];
   
    self.knowledgeClassView.backgroundColor = [UIColor whiteColor];
    [_knowledgeClassView release];
    self.knowledgeClassView.delegate = self;
    self.knowledgeClassView.dataSource = self;
    [self.view addSubview:self.knowledgeClassView];
    
    //注册cell
    [self.knowledgeClassView registerClass:[KnowledgeClassCell class] forCellWithReuseIdentifier:@"reuse"];
    
}

//协议方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.knowArr.count;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    KnowledgeClassCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuse forIndexPath:indexPath];
    Knowledge *know = self.knowArr[indexPath.row];
    cell.nameLabel.text = know.name;
    
    
    cell.picView.image = [UIImage imageNamed:self.pivArr[indexPath.row]];
        
    
    return cell;
}

//数据请求
-(void)PleaseData
{
    NSString *url = @"http://api.yi18.net/lore/loreclass";
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    [manger GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey:@"yi18"];
        for (NSMutableDictionary *tep in arr) {
            Knowledge *know = [[Knowledge alloc]init];
            [know setValuesForKeysWithDictionary:tep];
            [self.knowArr addObject:know];
            [know release];
        }
        if (self.knowArr.count > 0) {
            [[Singleton shareSingleton] hiddenYes];
        }
        [self.knowledgeClassView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"数据请求失败");
    }];
}
//页面跳转，知识信息列表
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    KnowMessageViewController *kvc = [[KnowMessageViewController alloc]init];
    Knowledge *know = self.knowArr[indexPath.row];
    kvc.id = know.id;
    kvc.title = know.name;
    [self.navigationController pushViewController:kvc animated:YES];
    [kvc release];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
